package com.example.fruit;

/**
 * Created by User on 2015/6/28.
 */

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

import com.example.fragment.HotFragment;
import com.example.fragment.NewProductFragment;
import com.example.fragment.PriceFragment;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;




public class GiftGuoSHiActivity extends ActionBarActivity implements ActionBar.TabListener {

    private ActionBar actionBar;
    private ViewPager viewPager;
    private List<Fragment> fragments;
    private int now;
    private RequestParams params;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgoushi);

        viewPager = (ViewPager) findViewById(R.id.ViewPagerId);
        params = getRquestParams();
        initActionBar();
        initFragments();
        viewPager
                .setAdapter(new MyFragmentAdapter(getSupportFragmentManager()));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageSelected(int arg0) {

                setTitle(actionBar.getTabAt(arg0).getText());
                actionBar.selectTab(actionBar.getTabAt(arg0));
                now = arg0;
            }
        });
    }

    private void initFragments() {

        fragments = new ArrayList<Fragment>();

        for (int i = 0, len = 3; i < len; i++) {
            Bundle args = new Bundle();

            if (i == 0) {
                Fragment cFragment = new NewProductFragment();
                args.putString("params", params.toString());
                cFragment.setArguments(args);
                fragments.add(cFragment);
            } else if (i == 1) {
                Fragment cFragment = new HotFragment();
                args.putString("params", params.toString());
                cFragment.setArguments(args);
                fragments.add(cFragment);
            } else if (i == 2) {
                Fragment cFragment = new PriceFragment();
                args.putString("params",params.toString());
                cFragment.setArguments(args);
                fragments.add(cFragment);
            }
        }
    }

    private void initActionBar() {
        actionBar = getSupportActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("新品")
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("热卖")
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("价格")
                .setTabListener(this));

    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }


    class MyFragmentAdapter extends FragmentPagerAdapter {
        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int poistion) {
            return fragments.get(poistion);
        }

    }




    private RequestParams getRquestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("page_size", "20");
        params.addBodyParameter("timestamp", "1435382308");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "");
        params.addBodyParameter("region_id", "143949");
        params.addBodyParameter("service", "product.category");
        params.addBodyParameter("device_id", "10b61d436aa7477f74006f903edb0f8a");
        params.addBodyParameter("channel", "qq");
        params.addBodyParameter("version", "2.1.1");
        params.addBodyParameter("sort", "0");
        params.addBodyParameter("sign", "f32e2d46abbe417aa286452a9f1362ff");
        params.addBodyParameter("class_id", "40");
        params.addBodyParameter("curr_page", "0");
        return params;

    }

}
